﻿/*
App.Config & Web.Config 示例

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="membase" type="Membase.Configuration.MembaseClientSection, Membase" />
  </configSections>
  <membase>
    <servers bucket="default" bucketPassword="555555" userName="Administrator" password="555555" retryCount="2" retryTimeout="00:00:02">
      <add uri="http://192.168.8.169:8091/pools/default" />
      <add uri="http://192.168.2.200:8091/pools/default" />
    </servers>
    <socketPool connectionTimeout="00:00:10" deadTimeout="00:00:10" />
  </membase>
</configuration>
 
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enyim.Caching.Memcached;
using Framework.Common.Cache;
using System.ComponentModel.Composition;
namespace Framework.Cache.Memcached
{
    [Export(typeof(ICache))]
    public class Memcached : ICache
    {
        Membase.MembaseClient mc;

        public Memcached()
        {
            mc = new Membase.MembaseClient();
        }

        public object Get(string key, DateTime newExpiredTime)
        {
            return mc.Get(key, newExpiredTime);
        }

        public T Get<T>(string key, DateTime newExpiredTime)
        {
            return mc.Get<T>(key, newExpiredTime);
        }

        public IDictionary<string, object> Get(IEnumerable<string> keys)
        {
            return mc.Get(keys);
        }

        public bool Set(string key, object value, TimeSpan ts)
        {
            return mc.Store(StoreMode.Set, key, value, ts);
        }

        public bool TryGet(string key, out object value)
        {
            return mc.TryGet(key, out value);
        }

        public bool TryGet(string key, out object value, DateTime newExpiredTime)
        {
            return mc.TryGet(key, newExpiredTime, out value);
        }

        public void Flush()
        {
            mc.FlushAll();
        }

        public bool Add(string key, object value)
        {
            return mc.Store(StoreMode.Set, key, value);
        }

        public bool Add(string key, object value, DateTime expiredTime)
        {
            return mc.Store(StoreMode.Set, key, value, expiredTime);
        }

        public bool Contains(string key)
        {
            return GetData(key) != null;
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public object GetData(string key)
        {
            return mc.Get(key);
        }

        public T GetData<T>(string key)
        {
            return mc.Get<T>(key);
        }

        public bool Remove(string key)
        {
            return mc.Remove(key);
        }
    }
}
